/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author egik
 */
public class LoginServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;    
    

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        // get request parameters for userID and password
        String user = request.getParameter("akun");
        String pwd = request.getParameter("password");    
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/fpkipem", "root", "");
            Statement st = con.createStatement();
            ResultSet rs;
            rs = st.executeQuery("SELECT * FROM `userdetails` WHERE username = '" + user + "' and password = '" + pwd + "'");
            if (rs.next()) {
                String Sts = rs.getString(3);
                System.out.println(Sts);
                HttpSession session = request.getSession();
                session.setAttribute("user", user);
                session.setAttribute("status",Sts);
                //setting session to expiry in 30 mins
                session.setMaxInactiveInterval(30 * 60);
                Cookie userName = new Cookie("user", user);
                Cookie Status = new Cookie("Status",Sts);
                userName.setMaxAge(30 * 60);
                Status.setMaxAge(30*60);
                response.addCookie(userName);
                response.addCookie(Status);
                if ("verifikasi".equals(Sts)) {
                    response.sendRedirect("lihatdaftar.jsp");
                } else {
                    response.sendRedirect("homedispenduk.jsp");
                }
            } else {
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.jsp");
                PrintWriter out = response.getWriter();
                out.println("<font color=red>Either user name or password is wrong.</font>");
                rd.include(request, response);
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}